//
//  InfoViewController.m
//  Nurse
//
//  Created by Sunny on 13-12-13.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "InfoViewController.h"
#import "InfoMsgCell.h"
#import "InfoMsgModel.h"
#import "InfoMsgDetailViewController.h"

@interface InfoViewController (){
    UIButton *btnEdit;
}
@property (nonatomic, strong) InfoMsgCell *lastSelectedCell;
@property (nonatomic, strong) NSMutableArray *arrInfoMsg;

@end

@implementation InfoViewController
@synthesize lastSelectedCell = _lastSelectedCell;
@synthesize arrInfoMsg = _arrInfoMsg;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title  = @"消息";
    // Do any additional setup after loading the view from its nib.
    btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnEdit setFrame:CGRectMake(0.f, 0.f, 40.f, 30.f)];
//    [btnEdit setImage:[UIImage imageNamed:@".png"] forState:UIControlStateNormal];
    [btnEdit setTitle:@"编辑" forState:UIControlStateNormal];
    [btnEdit setTitleColor:[UIColor colorWithRed:19.f / 255.f green:127 / 255.f blue:110.f / 255.f alpha:1.f] forState:UIControlStateNormal];
    [btnEdit setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [btnEdit addTarget:self action:@selector(editInfoMsgWithAnimate) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnEdit];
    [self.navigationItem setLeftBarButtonItem:leftBarItem];
    
    _arrInfoMsg = [NSMutableArray array];
    for (int i = 0; i < 100; i ++) {
        InfoMsgModel *md = [[InfoMsgModel alloc] init];
        [md setStrTitle:[NSString stringWithFormat:@"检查血压-%d",i]];
        [md setStrTime:[NSString stringWithFormat:@"12:30 --%d",i]];
        [md setStrDesc:@"惠氏倍力加营养配方粉，40岁以上人群专用的营养配方粉，全新年轻力配方提供四大功效,惠氏倍力加营养配方粉，40岁以上人群专用的营养配方粉，全新年轻力配方提供四大功效"];
        [_arrInfoMsg addObject:md];
        md = nil;
    }
    
    
    [self.tableVInfoContain setDelegate:(id<UITableViewDelegate>)self];
    [self.tableVInfoContain setDataSource:(id<UITableViewDataSource>)self];
    [self setNav];
    
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,
                          [UIFont boldSystemFontOfSize:19],UITextAttributeFont,
                          nil];
    
    self.navigationController.navigationBar.titleTextAttributes = dict;

    [App showTabbar];
}
-(void)setNav{
    //[TSMessage setDefaultViewController:[Router shared].navigationController];
    
    if([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
    {
        [[UINavigationBar appearance] setShadowImage:[UIImage new]];
    }
    
    UINavigationBar *navBar = self.navigationController.navigationBar;
    
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        //if iOS 5.0 and later
        
        [navBar setBackgroundImage:[UIImage imageNamed:@"head-bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
}


-(void)editInfoMsgWithAnimate{
    [self.tableVInfoContain setEditing:!self.tableVInfoContain.isEditing animated:YES];
    if (self.tableVInfoContain.isEditing) {
        [btnEdit setTitle:@"完成" forState:UIControlStateNormal];
    }else{
        [btnEdit setTitle:@"编辑" forState:UIControlStateNormal];
    }
    if (_lastSelectedCell) {
        [[_lastSelectedCell lblMsgTitle] setTextColor:[UIColor blackColor]];
        [[_lastSelectedCell lblMsgDesc] setTextColor:[UIColor darkGrayColor]];
        [_lastSelectedCell.contentView setBackgroundColor:[UIColor clearColor]];
    }
}

#pragma UITableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 62.f;
}


#pragma UITableView Datasource

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tableVInfoContain.isEditing) {
        return YES;
    }
    return NO;
}
//
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return UITableViewCellEditingStyleDelete;
//}
//
//- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
//    return NO;
//}
//
//
//- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}

//点击删除按钮后的回调
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"=========");
        [_arrInfoMsg removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删 除";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return [_arrInfoMsg count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];//@"Cell";
    InfoMsgCell * cell = (InfoMsgCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"InfoMsgCell" owner:self options:nil] objectAtIndex:0];
        [cell setSelected:YES animated:YES];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([_arrInfoMsg count]) {
        InfoMsgModel *model = [_arrInfoMsg objectAtIndex:indexPath.row];
        [cell.lblMsgTitle setText:model.strTitle];
        [cell.lblMsgTime setText:model.strTime];
        
        [cell.lblMsgDesc setText:[model.strDesc length] > 40 ? [[model.strDesc substringToIndex:40] stringByAppendingString:@"..."] : [model.strDesc stringByAppendingString:@"..."]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_lastSelectedCell) {
        [[_lastSelectedCell lblMsgTitle] setTextColor:[UIColor blackColor]];
        [[_lastSelectedCell lblMsgDesc] setTextColor:[UIColor darkGrayColor]];
        [_lastSelectedCell.contentView setBackgroundColor:[UIColor clearColor]];
    }
    InfoMsgCell *selectedCell = (InfoMsgCell *)[tableView cellForRowAtIndexPath:indexPath];
    [[selectedCell lblMsgTitle] setTextColor:[UIColor colorWithRed:19.f / 255.f green:127 / 255.f blue:110.f / 255.f alpha:1.f]];
    [[selectedCell lblMsgDesc] setTextColor:[UIColor colorWithRed:136.f / 255.f green:189 / 255.f blue:182.f / 255.f alpha:1.f]];
    [selectedCell.contentView setBackgroundColor:[UIColor colorWithRed:232.f / 255.f green:253 / 255.f blue:250.f / 255.f alpha:1.f]];
    _lastSelectedCell = selectedCell;
    
    InfoMsgModel *selectedMd = (InfoMsgModel *)[_arrInfoMsg objectAtIndex:indexPath.row];
    InfoMsgDetailViewController *infoDescVC = [[InfoMsgDetailViewController alloc]initWithNibName:@"InfoMsgDetailViewController" bundle:nil];
    [infoDescVC setSelectedMessageDescription:selectedMd];
    [self.navigationController pushViewController:infoDescVC animated:YES];
    infoDescVC = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
